package com.mjw.zstu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjw.zstu.entity.User;
import com.mjw.zstu.mapper.UserMapper;
import com.mjw.zstu.resultDto.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public List<Map> userMsg() {
        return userMapper.find();
    }
    public int delete(int id){
        return userMapper.deleteById(id);
    }
    public int register(User user)  {
        user.setStatus(0);
        return userMapper.insert(user);
    }
    public JsonResult compare(int user_id,String password){
        User user=userMapper.selectById(user_id);
        if(user.getPassword().equals(password)){
            return new JsonResult(200,"校验成功",null);
        }
        return new JsonResult(500,"校验失败",null);
    }
    public List<Map> getUserAddress(Integer id){
        return userMapper.getUserAddress(id);
    }
    public int insert(User user)
    {user.setStatus(0);
        return userMapper.insert(user);
    }
    public User login(User user){
        return userMapper.selectOne(new QueryWrapper<>(user));
    }
    public User find(User user){
        return userMapper.selectOne(new QueryWrapper<>(user));
    }
    public IPage<User> selectAll(int pageNum, int pageSize){
        Page page=new Page<User>(pageNum,pageSize);
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.select(User.class,i->!i.getColumn().equals("password"));
        return userMapper.selectMapsPage(page,userQueryWrapper);
    }
    public JsonResult list(){
        QueryWrapper<User> q=new QueryWrapper<>();
        q.select(User.class,i->!i.getColumn().equals("password"));
        q.orderByAsc("status");
        List l=userMapper.selectList(q);
        return new JsonResult(200,"获取成功",l);
    }
    public JsonResult white(int user_id){
        User user=new User();
        user.setStatus(0);
        user.setUser_id(user_id);
        userMapper.updateById(user);
        return new JsonResult(200,"操作成功",null);
    }
    public JsonResult update(User user){
        userMapper.updateById(user);
        User user1=userMapper.selectById(user.getUser_id());
        user1.setPassword(null);
        return new JsonResult(200,"修改成功",user1);
    }
    public JsonResult black(int user_id){
        User user =new User();
        user.setStatus(1);
        user.setUser_id(user_id);
        userMapper.updateById(user);
        return new JsonResult(200,"拉黑成功",null);
    }
}
